package org.fisco.bcos.evidence.gateway.controller;

import org.fisco.bcos.evidence.gateway.config.ResultCode;
import org.fisco.bcos.evidence.gateway.config.ResultJson;
import org.fisco.bcos.evidence.gateway.entity.bcos.EvidenceReq;
import org.fisco.bcos.evidence.gateway.service.EvidenceService;
import org.fisco.bcos.evidence.gateway.utils.string.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@RestController
public class EvidenceController extends BaseController {
    @Resource
    private EvidenceService evidenceService;

    public final Logger logger = LoggerFactory.getLogger(this.getClass());

    /**
     * 存证接口
     *
     * @param param
     * @param request
     */
    @RequestMapping(method = {RequestMethod.POST}, value = "/evidence")
    @ResponseBody
    public ResultJson evidence(@Validated @RequestBody EvidenceReq param,
                               HttpServletRequest request) {
        logger.info("/evidence param="+param);
        if(StringUtils.isBlank(param.getEvidenceId()) || StringUtils.isBlank(param.getUser()) ){
            return ResultJson.failure(ResultCode.BAD_REQUEST, "");
        }
        String resp = null;
        try {
            if (param.getVersion().equals("1.0")) {
                //todo, old evidence
                //resp = evidenceService.evidenceV1_0(param);
            } else if (param.getVersion().equals("1.1")) {
                resp = evidenceService.evidence1_1(param);
            }

            if (resp == null) {
                return ResultJson.failure(ResultCode.UpBlockchainException);
            }
            if (resp.toString().equals(ResultCode.BAD_REQUEST.getMsg())) {
                return ResultJson.failure(ResultCode.BAD_REQUEST, resp);
            }
            if (resp.toString().equals(ResultCode.UpAccountBlockchainException.getMsg())) {
                return ResultJson.failure(ResultCode.UpAccountBlockchainException, resp);
            }
            if (resp.toString().equals(ResultCode.UpEvidenceBlockchainException.getMsg())) {
                return ResultJson.failure(ResultCode.UpEvidenceBlockchainException, resp);
            }
        } catch (Exception e) {
            return ResultJson.failure(ResultCode.ExecutionException, e.getMessage());
        }
        return ResultJson.ok(resp, "Success");
    }

    @RequestMapping(method = {RequestMethod.POST}, value = "/evidenceWithFile")
    public Object evidenceWithFile(@RequestParam(value = "file") MultipartFile file,
                                   @RequestParam(value = "user") String user,
                                   @RequestParam(value = "userPlatform", required = false) String userPlatform,
                                   @RequestParam(value = "evidenceId") String evidenceId,
                                   @RequestParam(value = "description", required = false) String description,
                                   @RequestParam(value = "data") String data) {
        logger.info("/evidenceWithFile user="+user+",userPlatform="+userPlatform+",evidenceId="+evidenceId
                +",description="+description+",data="+data);
        if(StringUtils.isBlank(user) || StringUtils.isBlank(evidenceId) ){
            return ResultJson.failure(ResultCode.BAD_REQUEST, "");
        }
        String resp = null;
        try {
            resp = evidenceService.evidenceWithFile1_1(userPlatform, user, evidenceId, description, data, file);
            if (resp == null) {
                return ResultJson.failure(ResultCode.UpBlockchainException);
            }
            if (resp.toString().equals(ResultCode.BAD_REQUEST.getMsg())) {
                return ResultJson.failure(ResultCode.BAD_REQUEST, resp);
            }
            if (resp.toString().equals(ResultCode.UpAccountBlockchainException.getMsg())) {
                return ResultJson.failure(ResultCode.UpAccountBlockchainException, resp);
            }
            if (resp.toString().equals(ResultCode.UpEvidenceBlockchainException.getMsg())) {
                return ResultJson.failure(ResultCode.UpEvidenceBlockchainException, resp);
            }
//            if (resp.toString().equals("Text security check failed") || resp.toString().equals("Text is block")) {
//                return ResultJson.failure(ResultCode.ExamineCheckFailed, resp);
//            }
        } catch (Exception e) {
            return ResultJson.failure(ResultCode.ExecutionException, e.getMessage());
        }
        return ResultJson.ok(resp, "Success");
    }

//    //todo
//    @GetMapping("/queryByTxId/{txId}")
//    public ResultJson queryByTxId(@PathVariable String txId,
//                                  HttpServletRequest request) {
//        if (txId == null || txId.trim().length() <= 0) {
//            return ResultJson.failure(ResultCode.BAD_REQUEST);
//        }
//        Object blockInfo = null;
//        try {
//            logger.info("queryByTxId txId="+txId);
//            if (txId.length() == 64) {
//                blockInfo = evidenceService.queryByTxId(txId);
//            } else {
//                blockInfo = evidenceService.queryByTxId(txId.substring(2));
//            }
//            if (blockInfo == null) {
//                return ResultJson.ok();
//            }
//        } catch (Exception e) {
//            return ResultJson.failure(ResultCode.ExecutionException, e.toString());
//        }
//        return ResultJson.ok(blockInfo, "成功");
//    }

//    @RequestMapping(method = {RequestMethod.POST}, value = "/queryHistoryForKey")
//    @ResponseBody
//    public ResultJson queryHistoryForKey(@RequestBody EvidenceHistoryQueryReq param,
//                                         HttpServletRequest request) {
//        if (StringUtils.isBlank(param.getEvidenceId())) {
//            return ResultJson.failure(ResultCode.BAD_REQUEST);
//        }
//        Object historyInfo = null;
//        try {
//            logger.info("queryHistoryForKey param="+param.toString());
//            historyInfo = evidenceService.queryHistoryForKey(param);
//        } catch (Exception e) {
//            return ResultJson.failure(ResultCode.ExecutionException, e.getMessage());
//        }
//        return ResultJson.ok(historyInfo, "成功");
//    }

    @GetMapping("/queryHistoryForKey")
    public ResultJson queryHistoryForKey(
            @RequestParam(value = "evidenceId", required = true) String evidenceId) {
        logger.info("/queryHistoryForKey evidenceId="+evidenceId);
        if(StringUtils.isBlank(evidenceId) ){
            return ResultJson.failure(ResultCode.BAD_REQUEST, "");
        }
        Object historyInfo = null;
        try {
            historyInfo = evidenceService.queryHistoryForKey(evidenceId);
            if (historyInfo == null) {
                return ResultJson.ok();
            }
        } catch (Exception e) {
            return ResultJson.failure(ResultCode.ExecutionException, e.getMessage());
        }
        return ResultJson.ok(historyInfo, "Success");
    }


    @GetMapping("/queryEvidenceByUser")
    public ResultJson queryEvidenceByUser(@RequestParam(value = "userId", required = true) String userId) {
        logger.info("/queryEvidenceByUser userId="+userId);
        if(StringUtils.isBlank(userId)){
            return ResultJson.failure(ResultCode.BAD_REQUEST, "");
        }
        Object resp = null;
        try {
            resp = evidenceService.queryEvidenceByUser(userId);
            if (resp == null) {
                return ResultJson.ok();
            }
        } catch (Exception e) {
            return ResultJson.failure(ResultCode.ExecutionException, e.toString());
        }
        return ResultJson.ok(resp, "Success");
    }


//
//    @PostMapping("/queryEvidenceRecords")
//    public ResultJson queryEvidenceRecords(@Validated @RequestBody EvidenceRecordsQueryReq req,
//                                           HttpServletRequest request) {
//        Object resp = null;
//        try {
//            logger.info("queryEvidenceRecords req="+req);
//            resp = evidenceService.queryEvidenceRecords(req);
//            if (resp == null) {
//                return ResultJson.ok();
//            }
//        } catch (Exception e) {
//            return ResultJson.failure(ResultCode.ExecutionException, e.toString());
//        }
//        return ResultJson.ok(resp, "成功");
//    }
//
//    @GetMapping("/queryEvidenceAmount")
//    public ResultJson queryEvidenceAmount(HttpServletRequest request) {
//        Object resp = null;
//        try {
//            resp = evidenceService.queryEvidenceAmount();
//            if (resp == null) {
//                return ResultJson.ok();
//            }
//        } catch (Exception e) {
//            return ResultJson.failure(ResultCode.ExecutionException, e.toString());
//        }
//        return ResultJson.ok(resp, "成功");
//    }

}
